package org.ry.camera.adapter;
import java.util.ArrayList;
import java.util.List;

import org.ry.camera.R;
import org.ry.camera.activity.PhotoActivity;
import org.ry.camera.activity.PhotoActivity.OnChangeDeleteStatusListener;
import org.ry.camera.activity.PhotoActivity.OnItemClickListener;
import org.ry.camera.bean.PretendBean;
import org.ry.camera.tools.LogTool;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
/**
 * 
 * @author renyang
 *
 */
public class PhotoAdapter extends BaseAdapter implements OnChangeDeleteStatusListener
,OnItemClickListener{
	
	private List<PretendBean> mPretendList;
	
	private final LayoutInflater mInflater;
	
	private ViewHolder holder;
	/**
	 *  Invalid listViewHolder
	 */
	private List<ViewHolder> listViewHolder ;
	
	private String mSelected = "";
	
	public static final String CHECKED = "t@#$";
	
	private static final String UNCHECKED = "f@#$";
	
	private View view;
	
	public PhotoAdapter(List<PretendBean> mPretendList,Context mContext,View view){
		this.mPretendList = mPretendList;
		mInflater = LayoutInflater.from(mContext);
		((PhotoActivity)mContext).setOnChangeDeleteStatusListener(this);
		((PhotoActivity)mContext).setOnItemClickListener(this);
		//listViewHolder = new ArrayList<PhotoAdapter.ViewHolder>();
		this.view = view;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mPretendList.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return mPretendList.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		 if(convertView == null){
		 	 
			 convertView = mInflater.inflate(R.layout.item_photo, null);
		 	 
			 holder = new ViewHolder();
			 
			 holder.mImageView = (ImageView)convertView.findViewById(R.id.iv);
			 
			 holder.mCheckView = (ImageView)convertView.findViewById(R.id.check);
		 	 			 
			 convertView.setTag(holder);
		 }else{
		 	 holder = (ViewHolder) convertView.getTag();
		 }
		 
		 PretendBean mPretendBean =  mPretendList.get(position);
		 
		 holder.mImageView.setImageBitmap(mPretendBean.getmPic());
		 
		if(mSelected.contains(position+CHECKED)){
			holder.mCheckView.setBackgroundResource(R.drawable.btn_check_on_holo_light);
		}else{
			holder.mCheckView.setBackgroundResource(R.drawable.btn_check_off_holo_light);
		}
		
		if(view instanceof ListView){
			holder.mCheckView.setVisibility(View.VISIBLE);
		}else{
			holder.mCheckView.setVisibility(View.GONE);
		}
		
		return convertView;
	}
	
	static class ViewHolder{
		
		ImageView mImageView;
		ImageView mCheckView;
		
	}
	/**
	 *  
	 * @return true show delete 
	 */
	public boolean getCurrentStatus(){
		return holder.mCheckView.getVisibility() == View.VISIBLE;
	}
	
	@Override
	public void onChangeDeleteStatus(boolean isListView) {
		// TODO Auto-generated method stub
	}
	/**
	 * @deprecated
	 */
	private void visible(){
		for(ViewHolder mHolder : listViewHolder){
			if(mHolder!= null && mHolder.mCheckView != null){
				mHolder.mCheckView.setVisibility(View.VISIBLE);
			}
		}
	}
	/**
	 * @deprecated
	 */
	private void gone(){
		for(ViewHolder mHolder : listViewHolder){
			if(mHolder!= null && mHolder.mCheckView != null){
				mHolder.mCheckView.setVisibility(View.GONE);
			}
		}
	}
	/**
	 * @deprecated
	 */
	private void notifyData(){
		listViewHolder.clear();
		notifyDataSetChanged();
	}

	@Override
	public void onItemClick(int position) {
		// TODO Auto-generated method stub
		appendSelect(position);
		notifyDataSetChanged();
	}
	
	public String getDateMap(){
		return mSelected;
	}
	
	public void setDateMap(String mSelected){
		this.mSelected = mSelected;
	}

	public void setList(List<PretendBean> mPretendList){
		this.mPretendList = mPretendList;
	}
	public void setView(View view){
		this.view = view;
	}
	
	private void appendSelect(int position){
		if(mSelected.contains(position+CHECKED)){
			mSelected =	mSelected.replace(position+CHECKED, position+UNCHECKED);
		}else if(mSelected.contains(position+UNCHECKED)){
			mSelected = mSelected.replace(position+UNCHECKED, position+CHECKED);
		}else{
			mSelected = mSelected + position + CHECKED;
		}
	}
	
	@Override
	public void onAllSelect() {
		// TODO Auto-generated method stub
		mSelected = "";
		for(int p = 0 ; p < mPretendList.size() ; p++){
			appendSelect(p);
		}
		notifyDataSetChanged();
	}

	@Override
	public void onAllCancel() {
		// TODO Auto-generated method stub
		mSelected = "";
		notifyDataSetChanged();
	}
}
